package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.annotation.Log;
import org.example.domain.dto.DTO;
import org.example.domain.dto.PageDTO;
import org.example.domain.po.ErpRole;
import org.example.domain.po.ErpUser;
import org.example.domain.query.RoleQuery;
import org.example.domain.vo.ErpRoleVO;
import org.example.domain.vo.home.HomeDataVO;
import org.example.enums.BusinessType;
import org.example.service.ErpRoleService;
import org.example.service.impl.ErpRoleServiceImpl;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
@Api("角色管理")
public class ErpRoleController {
    @Autowired
    private ErpRoleService roleService;
    
    @GetMapping("/findRoleByTenantId")
    @ApiOperation("/根据租户id查询角色列表")
    public Result<List<ErpRole>> findRoleByTenantId(@RequestAttribute("user") ErpUser user) {
        return Result.success(roleService.findRoleByTenantId(user.getTenantId()));
    }
    
    @Log(operation = "角色", businessType = BusinessType.INSERT, isSaveRequestData = true, paramName = "formData", jsonPath = "$.name")
    @PostMapping("/addRole")
    @ApiOperation("/新增角色")
    public Result<String> addRole(@RequestBody ErpRole formData, @RequestAttribute("user") ErpUser user) {
        boolean save = roleService.insertRole(formData);
        return save ? Result.success("新增成功！") : Result.error("新增失败！");
    }
    
    @GetMapping("/findRoleOne")
    @ApiOperation("查询角色名称")
    public Result<Boolean> findRoleOne(String roleName, String type, @RequestAttribute ErpUser user){
        LambdaQueryWrapper<ErpRole> wrapper = new LambdaQueryWrapper<>();
        
        wrapper.eq(ErpRole::getName, roleName)
                .eq(ErpRole::getTenantId, user.getTenantId());
//        if (type.equals("0"))
        long count = roleService.count(wrapper);
        boolean h = count == 0;
        return Result.success(h);
    }
    
    @GetMapping("/page")
    @ApiOperation("条件查询租户下的角色")
    public Result<PageDTO<ErpRoleVO>> queryRolePage(RoleQuery roleQuery) {
        return Result.success(roleService.queryRolePage(roleQuery));
    }
    
    @Log(operation = "角色关联关系", businessType = BusinessType.INSERT)
    @PostMapping("/saveGrant")
    @ApiOperation("角色授权")
    public Result saveGrant(
            @RequestAttribute("user") ErpUser user,
            @RequestBody Map<String,Object> data){
        data.put("tenantId", user.getTenantId());
        boolean b = roleService.saveGrant(data);
        return b ?  Result.success() : Result.error("授权失败");
    }
    
    @Log(operation = "角色", businessType = BusinessType.UPDATE, isSaveRequestData = true, paramName = "role", jsonPath = "$.id")
    @PutMapping("/updateRole")
    @ApiOperation("修改角色信息")
    public Result updateRole(@RequestAttribute("user") ErpUser user ,@RequestBody ErpRole role){
        role.setTenantId(user.getTenantId());
        boolean b = roleService.updateRole(role);
        return b? Result.success() : Result.error("修改失败");
    }
    
    @Log(operation = "角色", businessType = BusinessType.DELETE, isSaveRequestData = true, paramName = "roleNames")
    @DeleteMapping("/roleRemoveBatch/{roleNames}")
    @ApiOperation("根据角色名集合删除角色")
    public Result remove(@PathVariable("roleNames") List<String> roleNames, @RequestAttribute ErpUser user){
        return roleService.removeBatchByNames(roleNames, user.getTenantId());
    }
    
    @Log(operation = "角色", businessType = BusinessType.STATUS, isSaveRequestData = true, paramName = "roleNames")
    @PutMapping("/upRoleEnabled/{roleNames}/{enabled}")
    @ApiOperation("修改角色状态")
    public Result updateRoleStatus(@PathVariable("roleNames") List<String> roleNames,
                                   @PathVariable("enabled") boolean enabled,
                                   @RequestAttribute ErpUser user){
        boolean b = roleService.updateRoleEnable(roleNames, enabled, user.getTenantId());
        return b ? Result.success() : Result.error("失败");
    }
    
    @GetMapping("getHomeData")
    @ApiOperation("获取首页数据")
    public Result<DTO<HomeDataVO>> getHomeData(@RequestAttribute ErpUser user){
        return Result.success(roleService.getHomeData(user));
    }
}
